package com.bowen.spike.controller;


import cn.hutool.core.util.IdUtil;
import com.bowen.spike.common.Result;
import com.bowen.spike.context.UserContext;
import com.bowen.spike.entity.Voucher;
import com.bowen.spike.service.IVoucherService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 优惠券表 前端控制器
 * </p>
 *
 * @author bowen
 * @since 2025-04-28
 */
@RestController
@RequestMapping("/api/voucher")
@RequiredArgsConstructor
public class VoucherController {

    private final IVoucherService voucherService;

    /**
     * 创建优惠券
     */
    @PostMapping
    public Result<Void> createVoucher(@RequestBody Voucher voucher) throws InterruptedException {
        voucher.setId(IdUtil.getSnowflake().nextId());
        voucherService.createVoucher(voucher);
        return Result.ok();
    }

    /**
     * 查询所有优惠券
     */
    @GetMapping
    public Result<List<Voucher>> getAllVouchers() {
        return Result.ok(voucherService.list());
    }

    /**
     * 根据ID查询优惠券
     */
    @GetMapping("/{id}")
    public Result<Voucher> getVoucherById(@PathVariable Long id) {
        Voucher voucher = voucherService.getById(id);
        return Result.ok(voucher);
    }

    /**
     * 更新优惠券
     */
    @PutMapping("/{id}")
    public Result<Void> updateVoucher(@PathVariable Long id, @RequestBody Voucher voucher) {
        voucher.setId(id);
        voucherService.updateById(voucher);
        return Result.ok();
    }

    /**
     * 删除优惠券
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteVoucher(@PathVariable Long id) {
        voucherService.removeById(id);
        return Result.ok();
    }



    /**
     * 修改优惠券状态
     */
    @PutMapping("/{id}/{status}")
    public Result<Void> updateVoucherStatus(@PathVariable Long id,@PathVariable Integer status) {
        voucherService.updateVoucherStatus(id,status);
        return Result.ok();
    }


}
